package com.rc.evcharger.service.tenant;

import com.rc.evcharger.common.constant.PlatformConstant;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.mapper.platform.PlatformSettingMapper;
import com.rc.evcharger.mapper.tenant.TenantSettingMapper;
import com.rc.evcharger.model.platform.PlatformSetting;
import com.rc.evcharger.model.platform.PlatformSettingExample;
import com.rc.evcharger.model.tenant.TenantSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Sven on 2021/03/03
 */
@Service
public class TenantSettingServiceImpl implements TenantSettingService {
    @Autowired
    private TenantSettingMapper tenantSettingMapper;

    @Autowired
    private PlatformSettingMapper platformSettingMapper;

    @Override
    public TenantSettingMapper getMapper() {
        return tenantSettingMapper;
    }

    @Override
    public BigDecimal query_start_charge_account_limit(String tenantCode) {
        //先取运营商的设置
        TenantSetting tenantSetting = tenantSettingMapper.selectByPrimaryKey(tenantCode);
        if (tenantSetting != null && tenantSetting.getStartChargeAccountMin().doubleValue() > 0) {
            return tenantSetting.getStartChargeAccountMin();
        } else {
            PlatformSetting platformSetting = selectSettingByParaName(PlatformConstant.START_CHARGE_ACCOUNT_LIMIT);
            if (platformSetting != null && (new BigDecimal(platformSetting.getParaValue())).doubleValue() > 0) {
                return new BigDecimal(platformSetting.getParaValue());
            } else {
                return PlatformConstant.DEFALUT_START_CHARGE_ACCOUNT_LIMIT;
            }
        }
    }

    @Override
    public BigDecimal query_stop_charge_account_limit(String tenantCode) {
        //先取运营商的设置
        TenantSetting tenantSetting = tenantSettingMapper.selectByPrimaryKey(tenantCode);
        if (tenantSetting != null && tenantSetting.getStopChargAccountMin().doubleValue() > 0) {
            return tenantSetting.getStopChargAccountMin();
        } else {
            PlatformSetting platformSetting = selectSettingByParaName(PlatformConstant.STOP_CHARGE_ACCOUNT_LIMIT);
            if (platformSetting != null && (new BigDecimal(platformSetting.getParaValue())).doubleValue() > 0) {
                return new BigDecimal(platformSetting.getParaValue());
            } else {
                return PlatformConstant.DEFAULT_STOP_CHARGE_ACCOUNT_LIMIT;
            }
        }
    }

    @Override
    public BigDecimal query_mult_charge_account_limit(String tenantCode) {
        //先取运营商的设置
        TenantSetting tenantSetting = tenantSettingMapper.selectByPrimaryKey(tenantCode);
        if (tenantSetting != null && tenantSetting.getStartMultChargeAccountMin().intValue() > 0) {
            return new BigDecimal(tenantSetting.getStartMultChargeAccountMin());
        } else {
            return PlatformConstant.DEFAULT_START_MULT_CHARGE_ACCMOUNT_MIN_LIMIT;
        }
    }

    @Override
    public boolean query_open_charge_limit_soc(String tenantCode, int type) {
        //先取运营商的设置
        TenantSetting tenantSetting = tenantSettingMapper.selectByPrimaryKey(tenantCode);
        //soc
        if (type == SocketConstant.CON0) {
            //是否充电中SOC限高 ,0打开 1不打开 默认0
            if (tenantSetting != null && SocketConstant.STR_CON0.equals(tenantSetting.getOpenChargeLimitSoc())) {
                return true;
            } else {
                return false;
            }
            //温度
        } else if (type == SocketConstant.CON1) {
            //是否充电中车辆电池温度限高 ,0打开 1不打开 默认0
            if (tenantSetting != null && SocketConstant.STR_CON0.equals(tenantSetting.getOpenChargeLimitBatteryTemp())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 根据参数名称取参数值
     *
     * @param paraName
     * @return
     */
    private PlatformSetting selectSettingByParaName(String paraName) {
        PlatformSettingExample example = new PlatformSettingExample();
        example.createCriteria().andParaNameEqualTo(paraName);
        List<PlatformSetting> list = platformSettingMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

}
